var Game = function() {
	var characters = {};
	/* ----------------
	   TEMPORARY
	---------------- */
	this.GetChar = function() {
		var mob_settings = get_character_settings('Mobby');
		var mob = new Character(mob_settings);
		mob.SetTarget('Maclovio');
		characters['Mobby'] = mob;
		log({type: 'enemy_appears', enemy: mob_settings});
		mob.Ai();
		return mob_settings;
	};
	/* ----------------
	   /TEMPORARY
	---------------- */

	var $game = $(this);
	var character = null;

	/* ----------------
	   PUBLIC
	---------------- */
	this.Send = function(params) {
		switch (params.command) {
			case 'set_character':
				return set_character(params);
			case 'set_target':
				return character.SetTarget(params.target);
			case 'get_character_settings':
				return ( character ) ? get_character_settings(character.Name) : false;
			/*case 'get_character_settings':
				return ( character ) ? get_character_settings(character.Name) : false;*/
			case 'queue_ability':
				return character.QueueAbility(params.ability);
			case 'apply_queue':
				return character.ApplyQueue();
			default:
				log({type: 'error', message: 'Unknown command ' + params.command});
		}
	};
	/* ----------------
	   /PUBLIC
	---------------- */

	var get_character_settings = function(name) {
		if ( !Toons[name] ) return false;

		var toon = Toons[name];
		var level = Levels[toon.Level];
		var race = Races[toon.Race];
		var job = Jobs[toon.Job];

		var moves = {};

		for ( var i = 1; i <= toon.Level; i++ ) {
			for ( var key in job[i].Moves ) {
				moves[key] = $.extend(job[i].Moves[key], toon.Moves[key]);
			}
		}

		toon = $.extend(toon, job[toon.Level], race[toon.Level], level);
		toon.Moves = moves;
		return toon;
	};

	var set_character = function(params) {
		if ( character ) {
			log({type: 'error', text: 'Character already loaded'});
			return false;
		}
		var toon = get_character_settings(params.name);
		if ( !toon ) {
			log({type: 'error', text: 'Unable to locate ' . params.name});
			return false;
		}

		character = new Character(toon);
		characters[character.Name] = character;
		log({type: 'character_set', name: character.Name});
		return true;
	};

	var log = function(params) {
		$game.trigger('message', params);
	};
};
